/*
 * 程序清单：动态定时器例程
 *
 * 这个例程会创建1个动态周期型定时器对象，然后控制它进行定时时间长度的更改。
 */
#include <rthw.h>
#include <rtthread.h>
#include "tc_comm.h"
#include "care.h"
#include "GPIB.h"
#include "led.h"
#include <string.h>

/* 定时器的控制块 */
static rt_timer_t timer1;
static rt_uint16_t WIFI_count=0;
static rt_uint8_t pre_sec=0;
static char wifi_char[128]; 


/* 定时器超时函数 */
static void timeout1(void* parameter)
{	
	if (PIN2_IS_SET(WIFI_LINK)&&PIN2_IS_SET(WIFI_nRTS)) 
	{
		rt_hw_led_on(0); 
	}
	else 
	{
		rt_hw_led_off(0);
	}
		
	if(GPIB_DELAY_COUNT>0) GPIB_DELAY_COUNT--;
	
	if(UART_Auto_Mode==1)
	{	
		switch(WIFI_TR1)
		{
			case 0x00:	//逃逸字符
				rt_device_write(rt_device_find("uart1"), 0, "+++", 3);
				AT_MODE = 1;
				WIFI_TR1 = 1;
				WIFI_count = 0;
				break;
			
			case 0x01:	//查询MAC
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+QMAC\r\n", 9);
					
					WIFI_TR1 = 2;
					WIFI_count = 0;
				}
				break;
			case 0x02:	//查询网络状态
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+LKSTT\r\n", 10);
					
					WIFI_TR1 = 6;
					WIFI_count = 0;
				}
				break;
			case 0x06:	//查询SOCKET状态
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+SKSTT=1\r\n", 12);
					
					WIFI_TR1 = 5;
					WIFI_count = 0;
				}
				break;
				
			case 0x03:	//reset wifi
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+Z\r\n", 6);			
			
					WIFI_TR1=9;
					WIFI_count=0;
				}
				break;
			case 0x04:	//恢复出厂设置
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+RSTF\r\n", 9);			
			
					WIFI_TR1=3;
					WIFI_count=0;
				}
				break;
			case 0x05:	//进入透传模式
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+ENTM\r\n", 9);			
			
					WIFI_TR1=9;
					WIFI_count=0;
				}
				break;
			case 0x09:	//结束等待模式
				if (WIFI_count++ >= 200)
				{
					UART_Auto_Mode=0;
					WIFI_TR1=0xFF;
					WIFI_count=0;
				}
				break;
				
			case 0x10:
				rt_device_write(rt_device_find("uart1"), 0, "+++", 3);
			
				WIFI_TR1 = 0x11;
				WIFI_count = 0;
				break;
			case 0x11:	//设置WIFI为STA模式
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+WPRT=0\r\n", 11);			
			
					WIFI_TR1=0x12;
					WIFI_count=0;
				}
				break;
			case 0x12:	//设置WIFI加密方式设置为WPA，WPA1-PSK/ WPA2-PSK 自动方式，WPA 相关的加密都可以使用这个方式
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+ENCRY=7\r\n", 12);			
			
					WIFI_TR1=0x13;
					WIFI_count=0;
				}
				break;
			case 0x13:	//设置WIFI连接的SSID
				if (WIFI_count++ >= 200)
				{
					//rt_device_write(rt_device_find("uart1"), 0, "AT+SSID=\"Tongliang\"\r\n", 21);			CARE_KEY
					strcpy(wifi_char,"AT+SSID=\"");
					strncat(wifi_char,CARE_SSID,strlen(CARE_SSID));
					strcat(wifi_char,"\"\r\n");
					rt_device_write(rt_device_find("uart1"), 0, wifi_char, strlen(wifi_char));
					WIFI_TR1=0x14;
					WIFI_count=0;
				}
				break;
			case 0x14:	//设置WIFI连接的密码
				if (WIFI_count++ >= 200)
				{
					//rt_device_write(rt_device_find("uart1"), 0, "AT+KEY=1,0,\"87654321\"\r\n", 23);			
					strcpy(wifi_char,"AT+KEY=1,0,\"");
					strncat(wifi_char,CARE_KEY,strlen(CARE_KEY));
					strcat(wifi_char,"\"\r\n");
					rt_device_write(rt_device_find("uart1"), 0, wifi_char, strlen(wifi_char));
					
					WIFI_TR1=0x15;
					WIFI_count=0;
				}
				break;
			case 0x15:	//启用DHCP
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+NIP=0\r\n", 10);			
			
					if(CARE_WIFI_MODE == 0)WIFI_TR1=0x16;
					if(CARE_WIFI_MODE == 1)WIFI_TR1=0x17;
					WIFI_count=0;
				}
				break;
			case 0x16:	//设置TCP连接，SERVER模式以及端口号
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+ATRM=0,1,120,5025\r\n", 22);			//AT+ATRM=0,0,"192.168.1.6",5025,5025 //AT+ATRM=0,1,120,5025
			
					WIFI_TR1=0x18;
					WIFI_count=0;
				}
				break;
			case 0x17:	//设置TCP连接，CLIENT模式以及端口号
				if (WIFI_count++ >= 200)
				{
					//rt_device_write(rt_device_find("uart1"), 0, "AT+ATRM=0,0,\"192.168.1.6\",5025,5025\r\n", 37);			//AT+ATRM=0,0,"192.168.1.6",5025,5025 //AT+ATRM=0,1,120,5025		
					strcpy(wifi_char,"AT+ATRM=0,0,\"");
					strncat(wifi_char,CARE_DMM,strlen(CARE_DMM));
					strcat(wifi_char,"\",5025\r\n");//,5025
					rt_device_write(rt_device_find("uart1"), 0, wifi_char, strlen(wifi_char));
					
					WIFI_TR1=0x18;
					WIFI_count=0;
				}
				break;
			case 0x18:	//保存参数
				if (WIFI_count++ >= 200)
				{
					rt_device_write(rt_device_find("uart1"), 0, "AT+PMTF\r\n", 9);			
			
					WIFI_TR1=0x03;//AT+Z reset模块
					WIFI_count=0;
				}
				break;
				
			case 0x20:	//逃逸字符
				rt_device_write(rt_device_find("uart1"), 0, "+++", 3);
			
				WIFI_TR1 = 0x04;
				WIFI_count = 0;
				break;
		}
	}
	if (PIN0_IS_CLR(SW1)) {SW_COUNT++;}
	if(LPC_RTC->SEC != pre_sec) {ms_int = 0; pre_sec = LPC_RTC->SEC;}else ms_int += 1;
}

void timer_control_init()
{
    /* 创建定时器1 */
    timer1 = rt_timer_create("timer1",  /* 定时器名字是 timer1 */
        timeout1, /* 超时时回调的处理函数 */
        RT_NULL, /* 超时函数的入口参数 */
        1, /* 定时长度，以OS Tick为单位，即10个OS Tick */
        RT_TIMER_FLAG_PERIODIC); /* 周期性定时器 */
    /* 启动定时器 */
    if (timer1 != RT_NULL)
        rt_timer_start(timer1);
    else
        tc_stat(TC_STAT_END | TC_STAT_FAILED);
}

#ifdef RT_USING_TC
static void _tc_cleanup()
{
    /* 调度器上锁，上锁后，将不再切换到其他线程，仅响应中断 */
    rt_enter_critical();

    /* 删除定时器对象 */
    rt_timer_delete(timer1);
    timer1 = RT_NULL;

    /* 调度器解锁 */
    rt_exit_critical();

    /* 设置TestCase状态 */
    tc_done(TC_STAT_PASSED);
}

int _tc_timer_control()
{
    /* 设置TestCase清理回调函数 */
    tc_cleanup(_tc_cleanup);

    /* 执行定时器例程 */
    count = 0;
    timer_control_init();

    /* 返回TestCase运行的最长时间 */
    return 100;
}

#endif
